package com.hzeport.bgs22emduty.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzeport.bgs22emduty.config.login.LoginCompoentService;
import com.hzeport.bgs22emduty.config.login.UserInfoDetailVO;
import com.hzeport.bgs22emduty.dto.subsidy.ParamSubSidyDetailDTO;
import com.hzeport.bgs22emduty.dto.subsidy.ParamSubsidySaveDTO;
import com.hzeport.bgs22emduty.dto.work.ScreenDutyWorkDTO;
import com.hzeport.bgs22emduty.dto.work.ScreenDutyWorkDetailDTO;
import com.hzeport.bgs22emduty.dto.work.ScreenDutyWorkRemoveDTO;
import com.hzeport.bgs22emduty.dto.work.ScreenDutyWorkSaveDTO;
import com.hzeport.bgs22emduty.entity.*;
import com.hzeport.bgs22emduty.enums.DTypeEnum;
import com.hzeport.bgs22emduty.enums.DeleteFlagEnum;
import com.hzeport.bgs22emduty.outbound.AppendixRepository;
import com.hzeport.bgs22emduty.outbound.BasePersonInfoRepository;
import com.hzeport.bgs22emduty.outbound.ParamDutyRepository;
import com.hzeport.bgs22emduty.outbound.ParamSubsidyRepository;
import com.hzeport.bgs22emduty.outbound.db.ParamSubsidyRepositoryImpl;
import com.hzeport.bgs22emduty.service.ParamSubsidyService;
import com.hzeport.bgs22emduty.service.ParamSubsidyService;
import com.hzeport.bgs22emduty.util.GuavacacheUtils;
import com.hzeport.bgs22emduty.vo.base.PageVO;
import com.hzeport.bgs22emduty.vo.file.FileVO;
import com.hzeport.bgs22emduty.common.base.BaseEntity;
import com.hzeport.bgs22emduty.common.base.BaseResult;
import com.hzeport.bgs22emduty.common.base.BaseServiceImpl;
import feign.Param;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 参数表-值班配置表：
此表中存储，各部门值班的参数配置
 *
 * @author code generator
 * @date 2022-12-02 11:21:59
 */
@Service
@Slf4j
public class ParamSubsidyServiceImpl extends BaseServiceImpl<ParamSubsidyRepositoryImpl, ParamSubsidy> implements ParamSubsidyService {
    @Resource
    private LoginCompoentService loginCompoentService;
    @Resource
    private AppendixRepository appendixRepository;
    @Resource
    private BasePersonInfoRepository basePersonInfoRepository;
    @Resource
    private ParamDutyRepository paramDutyRepository;
    @Resource
    private GuavacacheUtils guavacacheUtils;


    @Override
    public BaseResult delete(String id, UserInfoDetailVO userInfoDetailVO) {
        return BaseResult.ok(repository.removeById(id));
    }

    @Override
    public BaseResult<ParamSubSidyDetailDTO> detail(String id) {
        ParamSubsidy screenDutyWork=repository.getById(id);

        ParamSubSidyDetailDTO screenDutyWorkDetailDTO=new ParamSubSidyDetailDTO();
        BeanUtils.copyProperties(screenDutyWork,screenDutyWorkDetailDTO);
        if (screenDutyWork.getParamDutyId()!=null){
            ParamDuty paramDuty=guavacacheUtils.getParamDuty(screenDutyWork.getParamDutyId());
            if (paramDuty!=null){
                screenDutyWorkDetailDTO.setParamDutyId(DTypeEnum.TYPE_DAY_TIME.getValueByCode(paramDuty.getDType()));
            }
        }
        return BaseResult.ok(screenDutyWorkDetailDTO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResult saveData(ParamSubsidySaveDTO paramSubsidySaveVO, UserInfoDetailVO userInfoDetailVO) throws Exception {
        ParamSubsidy screenDutyWork = new ParamSubsidy();
        BeanUtil.copyProperties(paramSubsidySaveVO,screenDutyWork);
        LambdaUpdateWrapper<ParamSubsidy> countLambda=new LambdaUpdateWrapper<ParamSubsidy>()
                .eq(ParamSubsidy::getSType,paramSubsidySaveVO.getSType())
                .eq(ParamSubsidy::getParamDutyId,paramSubsidySaveVO.getParamDutyId())
                .eq(ParamSubsidy::getCustomId,userInfoDetailVO.getCustomscode());
        if (!StringUtils.isEmpty(paramSubsidySaveVO.getId())){
            countLambda.ne(ParamSubsidy::getId,paramSubsidySaveVO.getId());
        }
        int count=repository.count(countLambda);
        if (count>0){
            throw new Exception("同一关区同一值班时段同一值班类型只能配置一份补贴");
        }
        if (StringUtils.isEmpty(screenDutyWork.getId())) {
            screenDutyWork.setCustomId(userInfoDetailVO.getCustomscode());
            screenDutyWork.setCustomName(userInfoDetailVO.getCustomsName());
            screenDutyWork.setCreateBy(userInfoDetailVO.getUserId());
            screenDutyWork.setCreateTime(LocalDateTime.now());
            screenDutyWork.setUpdateBy(userInfoDetailVO.getUserId());
            screenDutyWork.setUpdateTime(LocalDateTime.now());
            screenDutyWork.setDelFlag(DeleteFlagEnum.NOT_DELETE.getValue());
            repository.save(screenDutyWork);
        }else {
            ParamSubsidy byId = repository.getById(screenDutyWork.getId());
            if (byId != null){
                screenDutyWork.setRecVersion(byId.getRecVersion());
            }
            screenDutyWork.setUpdateBy(userInfoDetailVO.getUserId());
            screenDutyWork.setUpdateTime(LocalDateTime.now());
            repository.updateById(screenDutyWork);
            appendixRepository.remove(new LambdaUpdateWrapper<Appendix>().eq(Appendix::getRelevanceId, screenDutyWork.getId()));
        }
        return BaseResult.ok();
    }

    @Override
    public BaseResult<Page<ParamSubsidy>> list(PageVO dto, UserInfoDetailVO userInfoDetailVO) {
        LambdaQueryWrapper<ParamSubsidy> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(userInfoDetailVO.getCustomscode()), ParamSubsidy::getCustomId, userInfoDetailVO.getCustomscode());
        Page<ParamSubsidy> page = repository.page(new Page<>(dto.getPageNum(), dto.getPageSize()), queryWrapper);
        page.getRecords().forEach(screenDutyWork->{
            if (screenDutyWork.getParamDutyId()!=null){
                ParamDuty paramDuty=guavacacheUtils.getParamDuty(screenDutyWork.getParamDutyId());
                if (paramDuty!=null){
                    screenDutyWork.setParamDutyId(DTypeEnum.TYPE_DAY_TIME.getValueByCode(paramDuty.getDType()));
                }
            }
        });
        return BaseResult.ok(page);
    }
}
